import 'package:hxdj/common/base_page/base_page.dart';
import 'package:hxdj/common/dio/dio_http.dart';
import 'dart:convert';

class RecommendProjectViewModel extends BaseViewModel {
  // 请求的数据.
  List<ProjectModel> dataList = [];

  @override
  requestData({Function(dynamic)? onSuccess, Function(dynamic)? onError, bool isFresh = false}) async {
    dataList.clear();
    if (isFresh) {
      setState(ViewState.loading);
    }

    // 去请求服务器数据.
    Map<String, dynamic> resultMap = await DioHttpManager().request(MyApi.getShopRecommentObject,
        method: DioHttpManager.post, data: {"min_price": "98", "max_price": "998", "limit": "4"});

    if (resultMap['code'] == 0) {
      List<dynamic> resultList = resultMap["data"] ?? [];

      for (var element in resultList) {
        dataList.add(ProjectModel.fromMap(element));
      }
      // 更新页面.
      if (dataList.isEmpty) {
        setState(ViewState.empty);
      } else {
        setState(ViewState.normal);
      }
    } else {
      // 更新页面.
      setState(ViewState.error);
    }
  }
}

class ProjectModel {
  final int? id;
  final String? name;
  final String? logo;
  final String? otherCoverImg;
  final String? price;
  final String? serviceDuration;
  final String? desc;
  final String? anmoRoadMoneyTip;
  final String? goodsServiceFeeStr;
  final int? isDiscounts;
  final int? sales;

  
  final double? discountsRate;
  final String? goodsDiscountPrice;
  final int? isVip;
  final String? userChoseStr;
  final String? titleDesc;

  ProjectModel({
    this.id,
    this.name,
    this.logo,
    this.otherCoverImg,
    this.price,
    this.serviceDuration,
    this.desc,
    this.anmoRoadMoneyTip,
    this.goodsServiceFeeStr,
    this.isDiscounts,
    this.discountsRate,
    this.goodsDiscountPrice,
    this.isVip,
    this.userChoseStr,
    this.titleDesc,
    this.sales,
  });

  factory ProjectModel.fromJson(String str) => ProjectModel.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory ProjectModel.fromMap(Map<String, dynamic> json) => ProjectModel(
        id: json["id"],
        name: json["name"],
        logo: json["logo"],
        otherCoverImg: json["other_cover_img"],
        price: json["price"],
        serviceDuration: json["service_duration"],
        desc: json["desc"],
        sales:json['sales'],
        anmoRoadMoneyTip: json["anmo_road_money_tip"],
        goodsServiceFeeStr: json["goods_service_fee_str"],
        isDiscounts: json["isDiscounts"],
        discountsRate: json["discountsRate"]?.toDouble(),
        goodsDiscountPrice: json["goodsDiscountPrice"],
        isVip: json["isVip"],
        userChoseStr: json["user_chose_str"],
        titleDesc: json["title_desc"],
      );

  Map<String, dynamic> toMap() => {
        "id": id,
        "name": name,
        "logo": logo,
        "other_cover_img": otherCoverImg,
        "price": price,
        "service_duration": serviceDuration,
        "desc": desc,
        "anmo_road_money_tip": anmoRoadMoneyTip,
        "goods_service_fee_str": goodsServiceFeeStr,
        "isDiscounts": isDiscounts,
        "discountsRate": discountsRate,
        "goodsDiscountPrice": goodsDiscountPrice,
        "isVip": isVip,
        "user_chose_str": userChoseStr,
        "title_desc": titleDesc,
        "month_sales": sales,
      };
}
